// pages/scenter/addphone.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:null,
    code:null,
    allcode:0,
    verification:'textfocus-bottom-middle-but',
    msg:'未绑定手机号',
    bd:1,
    send:true,
    alreadySend:false,
    second: 60,
  },
  //获得手机号
  getphone(e){
    let that = this
    console.log(e.detail.value)
    that.setData({
      phone:e.detail.value
    })
  },
  getcode(e){
    let that = this
    console.log(e.detail.value)
    that.setData({
      code:e.detail.value
    })
  },
  /*验证码点击后边颜色*/
  verification(e){
    let that = this;
    let send = that.data.send
    if(send){
    if(that.data.phone==null){
      wx.showToast({
        title: '请输入手机号',
        icon: 'error',
        duration: 2000
      })
    }else{
      let unverification='textfocus-bottom-middle-unbut';
      that.setData({
        verification:unverification
      })
       //发送验证码
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
       //请求后台的分页数据
       cmd:'SendYzm',
       mobile:that.data.phone
      },
      method: "get",
      success(res) {
       var data = res.data.DATA;
       console.log(res);
        if(res.data.STATUS=='Success'){
          wx.showToast({
            title: '获得验证码成功',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            alreadySend:true,
            send:false
          })
          that.timer()
        }else{
          wx.showToast({
            title: '请重新获取',
            icon: 'error',
            duration: 2000
          })


          that.setData({
            verification:'textfocus-bottom-middle-but'
          })
        }
      }
   })
    }
    }
  },
  //60s倒计时
  timer(e) {
    let that = this;
    let promise =new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          that.setData({
            second:that.data.second - 1
          })
          if (that.data.second <= 0) {
            that.setData({
              second: 60,
              alreadySend:false,
              send:true
            })
            resolve(setTimer)
          }
        }
        , 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },
  //确认绑定
  okbuyphone(e){
    let that = this
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
       //请求后台的分页数据
       cmd:'bindMobile',
       mobile:that.data.phone,
       token:that.data.token,
       yzm:that.data.code
      },
      method: "get",
      success(res) {
        if(res.data.STATUS=='Success'){
          wx.showToast({
            title: '绑定手机号成功',
            icon: 'success',
            duration: 2000
          })
          wx.login({
            success (res) {
              wx.getUserInfo({
                success: function(b) {
                  wx.setStorageSync('wxinfo',b.userInfo);
                  wx.request({
                    url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
                    data: {
                      code: res.code,
                      cmd: 'wxlogin',
                      officeid:1
                    },
                    header: {
                      'content-type': 'application/json' // 默认值
                    },
                    success (data) {
                      if(data.STATUS='Success'){
                        wx.setStorageSync('userInfo',data.data.DATA);
                        wx.setStorageSync('token',data.data.DATA.token);
                            }
                    }
                  });
                }
              })
            }
          })
        }else{
          wx.showToast({
            title: '绑定手机号失败',
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },

 //返回上一页
 navigateBack() {
  wx.navigateBack({
    delta: 1
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let token = wx.getStorageSync('token')
    let userInfo = wx.getStorageSync('userInfo')
    if(userInfo.mobile!=null){
      that.setData({
        phone:userInfo.mobile,
        bd:2
      })
    }


    that.setData({
      verification:'textfocus-bottom-middle-but',
      token:token
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})